package main

import (
	"cade/src/bot/app"
	"cade/src/bot/handler"
	"cade/src/core"
	"cade/src/core/evtmgr"
	"cade/src/core/log"
	"flag"
	"math/rand"
	_ "net/http/pprof"
	"os"
	"time"
)

func main() {
	rand.Seed(time.Now().Unix())

	// parse flags
	addr := flag.String("addr", "ws://127.0.0.1:23001/gate", "server address")
	name := flag.String("name", "admin", "username to use")
	flag.Parse()

	// open log
	log.Open("")

	// signal
	quit := make(chan int, 1)
	core.OnSignal(func(s os.Signal) {
		log.Warning("shutdown signal received ...")
		quit <- 0
	})

	evtmgr.On("all.bot.dies", func(...interface{}) {
		log.Warning("ALL bots dies ...")
		quit <- 0
	})

	// start
	handler.Init()
	app.Start(*addr, *name)

	// wait
	<-quit

	// stop
	app.Stop()

	// close log
	log.Notice("bot stopped")
	log.Close()
}
